/**
 * Copyright (c) 快宝网络 kuaidihelp.com Co., Ltd. All Rights Reserved 禁止外泄以及用于其它的商业用途
 */

import { AREA, Options } from '@/components/_pages/jdk';
import request from '@/utils/request';
import type { OptionsType } from '@/components/_pages/jdk/addLeague/_utils/type';
import { isArray } from 'lodash';

export async function getLeagueList(params: any) {
  return request('/jdk/League/list', {
    method: 'POST',
    data: params,
  });
}

export async function saveLeague(params: any) {
  return request('/jdk/League/save', {
    method: 'POST',
    data: params,
  });
}

export async function getChildrenArea(
  params: { id: string | number },
  isLeaf = false,
): Promise<Options[]> {
  return request('/Inn/getChildrenArea', {
    method: 'POST',
    data: params,
  }).then(({ data }: { data: AREA[] }) => {
    if (data && isArray(data)) {
      return data.map((item) => ({
        label: item.name,
        value: item.id,
        code: item.code,
        isLeaf,
      }));
    }
    return [];
  });
}

export async function getAddress(params: any) {
  return request('/jdk/getAddress', {
    method: 'POST',
    data: params,
  });
}

export async function getFreightConfigInfo(params: any) {
  return request('/jdk/League/freightConfigInfo', {
    method: 'POST',
    data: params,
  });
}

export async function saveFreightConfigSave(params: any) {
  return request('/jdk/League/freightConfigSave', {
    method: 'POST',
    data: params,
  });
}

export async function getDetailData(params: any) {
  return request('/jdk/League/detail', {
    method: 'POST',
    data: params,
  });
}

let feeBrandList: OptionsType[] = [];
export async function getFreeServiceFeeBrandList(params: any) {
  return request('/jdk/League/getFreeServiceFeeBrandList', {
    method: 'POST',
    data: params,
  }).then((res) => {
    const { data } = res;
    feeBrandList = data.map((item: { name: any; code: any }) => ({
      label: item.name,
      value: item.code,
      ...item,
    }));
    return feeBrandList || [];
  });
}

export async function setFreeServiceFeeBrandList(params: any) {
  return request('/jdk/League/setFreeServiceFeeBrandList', {
    method: 'POST',
    data: params,
  });
}
